<template>
<div class="header">
  <div class="wbg">
    <div class="header-top w">
      <div class="header-user">尚品汇欢迎您！！！
        <span v-if="name">{{name}}  <span @click="logout">退出登录</span></span>
        <span v-else>
          请<router-link to="/login">登录</router-link> |
          <router-link to="/register">免费注册</router-link>
        </span> 
        
      </div>
      <ul class="header-nav">
        <li>
          <router-link to="/center">我的订单</router-link>
        </li>
        <li>
          <router-link to="/shopcart">我的购物车</router-link>
        </li>
        <li>我的尚品汇</li>
        <li>尚品汇会员</li>
        <li>企业采购</li>
        <li>关注尚品汇</li>
        <li>合作招商</li>
        <li class="header-nav-last">商家后台</li>
      </ul>
    </div>
  </div>
    <div class="header-bottom w">
      <router-link to="/">
        <img class="header-logo" src="./images/logo.png" alt="logo" />
      </router-link>
      <form class="header-search" @submit.prevent="">
        <input type="text" v-model="keyword"/>
        <button @click="handelSerch">搜索</button>
      </form>
    </div>
</div>
</template>

<script>
import { mapActions, mapState } from 'vuex'

export default {
  name: 'Header',
  data(){
    return {
      keyword:this.$route.params.keyword
    }
  },
  computed:{
    ...mapState("login",["name"])
  },
  methods:{
    ...mapActions("login",["logout"]),
    handelSerch(){
      const {keyword} = this

      const location ={
        name:"Serch",
        query:this.$route.query
      }
      if(keyword){
        location.params = {keyword}
        
        // location.query.keyword = keyword
      }
      // console.log("前",location);
      
      this.$router.history.push(location)
    },
  },
  watch:{
    $route({params}){
      this.keyword = params.keyword
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.wbg{
  background-color: #eaeaea;
}
.w{
  margin: 0 auto;
  width: 1200px;
}
.header-top {
  width: 1200px;
  padding: 10px 0px;
  display: flex;
  justify-content: space-between;
}
// 嵌套样式一般最多2-3层
.header-nav {
  display: flex;
  li {
    margin-left: 10px;
    padding-right: 10px;
    border-right: 1px solid #000;
    // &:last-child {
    //   border-right: none;
    //   padding-right: 0;
    // }
  }
  .header-nav-last {
    border-right: none;
    padding-right: 0;
  }
}
/*
  .a .b .c { ... }
  浏览器从右往左解析css选择器
*/
.header-bottom {
  // padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-search {
  display: flex;
  align-items: center;
  input {
    box-sizing: border-box;
    border: 1px solid red;
    padding-left: 10px;
    outline: none;
    width: 450px;
    height: 30px;
    font-size: 16px;
  }
  button {
    width: 80px;
    height: 30px;
    background-color: red;
    color: #fff;
    border: none;
  }
}
</style>
